import { Color } from 'https://unpkg.com/three/build/three.module.js';
import { initShaders } from '../jsm/Utils.js';
import lib from '../jsm/ThreeLib.js';

window.onload = function () {

    const backColor = new Color("rgba(233,233,233,1)");

    const canvas = document.getElementById("canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    const gl = canvas.getContext("webgl");
    //初始化着色器
    initShaders(gl,
        document.querySelector("#vertexShader").innerText,
        document.querySelector("#fragmentShader").innerText);

    lib.clearCanvas(gl, backColor);

    const a_Position = gl.getAttribLocation(gl.program, "a_Position");

    canvas.addEventListener('click', function (event) {
        const { clientX, clientY } = event;
        const { left, top, width, height } = canvas.getBoundingClientRect();
        const [cssX, cssY] = [clientX - left, clientY - top];

        var [x, y] = [lib.GetPosiOfX(width, cssX), lib.GetPosiOfY(height, cssY)];


        gl.vertexAttrib3f(a_Position, x, y, 0.0);


        lib.clearCanvas(gl, backColor);
        //这个函数在绘制的时候回清空背景
        gl.drawArrays(gl.POINTS, 0, 1);
    })


}